package com.test.qpbgboot.backend.controller;

import com.test.qpbgboot.backend.pojo.dto.BackendAccountLoginParam;
import com.test.qpbgboot.backend.pojo.dto.BackendAccountQuery;
import com.test.qpbgboot.backend.pojo.dto.BackendAccountSaveParam;
import com.test.qpbgboot.backend.pojo.dto.OperationLogQuery;
import com.test.qpbgboot.backend.pojo.vo.BackendAccountVO;
import com.test.qpbgboot.backend.pojo.vo.OperationLogVO;
import com.test.qpbgboot.backend.service.BackendService;
import com.test.qpbgboot.base.response.JsonResult;
import com.test.qpbgboot.base.utils.JwtTokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/v1/backend/account")
@Slf4j
@Api(tags = "backend module")
public class BackendController {

    @Autowired
    private BackendService backendService;

    @GetMapping("/select")
    @ApiOperation(value = "后台账号查询")
    public JsonResult selectBackendAccount(
            @Valid @ModelAttribute BackendAccountQuery backendAccountQuery) {
        log.debug("后台账号查询:backendAccountQuery={}", backendAccountQuery);
        List<BackendAccountVO> backendAccountVOList = backendService
                .backendAccountSelect(backendAccountQuery);
        return JsonResult.success(backendAccountVOList);
    }

    @PostMapping("/save")
    @ApiOperation(value = "后台账号保存")
    public JsonResult saveBackendAccount(
            @Valid @RequestBody BackendAccountSaveParam backendAccountSaveParam) {
        log.debug("后台账号保存:backendAccountSaveParam={}", backendAccountSaveParam);
        backendService.backendAccountSave(backendAccountSaveParam);
        return JsonResult.success();
    }

    @PostMapping("/login")
    @ApiModelProperty(value = "后台账号登录")
    public ResponseEntity<JsonResult> login(
            @Valid
            @RequestBody
            BackendAccountLoginParam backendAccountLoginParam) {
        log.debug("后台账号登录:backendAccountLoginParam={}",
                backendAccountLoginParam);
        BackendAccountVO backendAccountVO =
                backendService.backendAccountLogin(backendAccountLoginParam);
        return ResponseEntity.ok()
                .header("Authorization", "Bearer " +
                        JwtTokenUtils.createJWT(backendAccountVO, 30))
                .body(JsonResult.success(backendAccountVO));
    }

    @PostMapping("/log-off/{accountId}")
    @ApiOperation(value = "后台账号注销")
    public JsonResult backendAccountLogOff(
            @PathVariable @ApiParam(value = "后台账号id", name = "accountId",
                    required = true) Long accountId) {
        log.debug("后台账号注销:accountId={}", accountId);
        backendService.backendAccountLogOff(accountId);
        return JsonResult.success();
    }

    @GetMapping("/oper-log/select")
    @ApiOperation(value = "操作日志查询")
    public JsonResult operationLogSelect(
            @Valid @ModelAttribute OperationLogQuery operationLogQuery) {
        log.debug("操作日志查询:operationLogQuery={}", operationLogQuery);
        List<OperationLogVO> operationLogVOList =
                backendService.operationLogSelect(operationLogQuery);
        return JsonResult.success(operationLogVOList);
    }
}
